package com.happyfamily.springboot.crowdsourcingplatform.controller.wang;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Rule;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.RuleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 规则控制类
 */
@Controller
@Slf4j
public class RuleController
{
	@Autowired
	private RuleService ruleServiceImpl;

	/**
	 * 前端规则中心显示
	 * @return
	 */
	@ApiOperation(value = "前端规则中心显示", notes = "前端规则中心显示")
	@OperationLogDetail(detail = "前端规则中心显示", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	@GetMapping("/rule/ruleList")
	@ResponseBody
	public List<Rule> exposureList()
	{
		return ruleServiceImpl.ruleList();
	}


	/**
	 * 后台规则表格显示管理
	 * @param request
	 * @param uname
	 * @param rtitle
	 * @param rcontent
	 * @param rtime
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:rule:query')")
	@ApiOperation(value = "后台规则表格显示管理", notes = "后台规则表格显示管理")
	@OperationLogDetail(detail = "后台规则表格显示管理", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-back/rule/ruleTable")
	@ResponseBody
	public Results<Rule> ruleTable(PageTableRequest request, String uname, String rtitle, String rcontent, String rtime)
	{
		request.countOffset();
		return ruleServiceImpl.ruleTable(request.getOffset(), request.getLimit(), uname, rtitle, rcontent, rtime);
	}

	/**
	 * 后端对规则的删除
	 * @param rid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:rule:delete')")
	@ApiOperation(value = "后端对规则的删除", notes = "后端对规则的删除")
	@OperationLogDetail(detail = "后端对规则的删除", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	@PostMapping("/api-back/rule/ruleDelete")
	@ResponseBody
	public String ruleDelete(String rid)
	{
		return ruleServiceImpl.ruleDelete(rid);
	}

	/**
	 * 后端对规则新增
	 * @param titles
	 * @param contents
	 * @param req
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:rule:add')")
	@ApiOperation(value = "后端对规则新增", notes = "后端对规则新增")
	@OperationLogDetail(detail = "后端对规则新增", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	@PostMapping("/api-back/rule/ruleAdd")
	@ResponseBody
	public String ruleAdd(String titles, String contents, HttpServletRequest req)
	{
		return ruleServiceImpl.ruleAdd(titles, contents, req);
	}

	/**
	 * 后端管理员对规则进行修改
	 * @param rid
	 * @param titles
	 * @param contents
	 * @param backTitle
	 * @param req
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:rule:updata')")
	@ApiOperation(value = "后端管理员对规则进行修改", notes = "后端管理员对规则进行修改")
	@OperationLogDetail(detail = "后端管理员对规则进行修改", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/rule/ruleUpdata")
	@ResponseBody
	public String ruleUpdata(String rid, String titles, String contents, String backTitle, HttpServletRequest req)
	{
		return ruleServiceImpl.ruleUpdata(rid, titles, contents, backTitle, req);
	}

	/**
	 * 后端管理员对规则进行修改前的回显
	 * @param rid
	 * @return
	 */
	@ApiOperation(value = "后端管理员对规则进行修改前的回显", notes = "后端管理员对规则进行修改前的回显")
	@OperationLogDetail(detail = "后端管理员对规则进行修改前的回显", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@PostMapping("/api-back/rule/ruleReview")
	@ResponseBody
	public Rule ruleReview(String rid)
	{
		return ruleServiceImpl.ruleReview(rid);
	}

}
